package pers.qianyu.diytomcat.http;

import pers.qianyu.diytomcat.catalina.*;

import javax.servlet.*;
import java.io.*;

/**
 * @author mizzle rain
 * @date 2021-01-14 22:07
 */
public class ApplicationRequestDispatcher implements RequestDispatcher {
    private String uri;

    public ApplicationRequestDispatcher(String uri) {
        if (!uri.startsWith("/")) {
            uri = "/" + uri;
        }
        this.uri = uri;
    }

    @Override
    public void forward(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        Request req = (Request) servletRequest;
        Response res = (Response) servletResponse;

        req.setUri(uri);
        res.resetBuffer();
        HttpProcessor httpProcessor = new HttpProcessor();
        httpProcessor.execute(req.getSocket(), req, res);
        req.setForward(true);
    }

    @Override
    public void include(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

    }
}
